<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Request;
use think\View;
use think\Db;
use app\index\validate\Valid;
use PHPExcel;
use PHPExcel_Writer_Excel5;
use PHPExcel_Style_Alignment;
use PHPExcel_Style_Fill;
use PHPExcel_Style_Border;

class Tifeedback extends Commonwork
{   
    //作废
    public function zuofei($id,$from=1){
        $this->assign("from",$from);
        $this->assign("id",$id);
        $data = Db::table('all_ti_feeback_list t')
            ->join('cg_ti t2','t2.id=t.apply_id')
            ->order("t.id desc")
            ->where('t.id',$id)
            ->field('t.*,t2.title,t2.cate_id,t2.type_id,t2.sn t2sn,t2.id t2id,t2.brief,t2.fb_brief,t2.jxs_id,t2.type_id')
            ->find();

        $data['cate_title'] = db('cg_ti_cate')->where('id',$data['cate_id'])->value('title');
        $data['cate_title2'] = db('cg_ti_type')->where('id',$data['type_id'])->value('title');
        $data['fankuiren'] = db('employee')->where('id',$data['c_user_id'])->value('em_name');
        $data['fankuirenjob'] = db('job t')->join('job_employee t2','t2.job_id=t.id')->where('employee_id',$data['c_user_id'])->value('t.job_name');
        $data['fankuirenbm'] = db('department t')->join('department_employee t2','t2.department_id=t.id')->where(['employee_id'=>$data['c_user_id'],'t2.index'=>1])->value('t.de_name');
        $principal = db('department t')->join('department_employee t2','t2.department_id=t.id')->where(['employee_id'=>$data['c_user_id'],'t2.index'=>1])->value('t.principal');
        $data['zhijieshangji'] = db('employee')->where('id',$principal)->value('em_name');
        $data['jxs_title'] = db('cg_jxs')->where('id',$data['jxs_id'])->value('title');
        $this->assign('data',$data);

        $list = db('cg_ti_list')->where('ti_id',$data['apply_id'])->select();
        $zj_yjws = 0;
        $zj_yjhs = 0;
        $zj_yjtr = 0;
        $zj_sjws = 0;
        $zj_sjhs = 0;
        $zj_sjtr = 0;
        $zj_yjxl = 0;
        $zj_sjxl = 0;
        foreach($list as $k=>$v){
            $v['brand_title'] = db('pro_brands')->where('id',$v['brand'])->value('brand_name');
            if($v['category']){
                $brand_id = db('pro_category')->where('id',$v['category'])->value('brand_id');
                $v['brand_title'] = db('pro_brands')->where('id',$brand_id)->value('brand_name');
            }
            if($v['series']){
                $category_id = db('pro_series')->where('id',$v['series'])->value('category_id');
                $brand_id = db('pro_category')->where('id',$category_id)->value('brand_id');
                $v['brand_title'] = db('pro_brands')->where('id',$brand_id)->value('brand_name');
            }
            if($v['sku']){
                $series_id = db('pro_sku')->where('id',$v['sku'])->value('series_id');
                $category_id = db('pro_series')->where('id',$series_id)->value('category_id');
                $brand_id = db('pro_category')->where('id',$category_id)->value('brand_id');
                $v['brand_title'] = db('pro_brands')->where('id',$brand_id)->value('brand_name');
            }
            if($v['product']){
                $sku_id = db('pro_products')->where('id',$v['product'])->value('sku_id');
                $series_id = db('pro_sku')->where('id',$sku_id)->value('series_id');
                $category_id = db('pro_series')->where('id',$series_id)->value('category_id');
                $brand_id = db('pro_category')->where('id',$category_id)->value('brand_id');
                $v['brand_title'] = db('pro_brands')->where('id',$brand_id)->value('brand_name');
            }
            if($v['tm_id']){
                $sku_id = db('pro_products')->where('id',$v['tm_id'])->value('sku_id');
                $series_id = db('pro_sku')->where('id',$sku_id)->value('series_id');
                $category_id = db('pro_series')->where('id',$series_id)->value('category_id');
                $brand_id = db('pro_category')->where('id',$category_id)->value('brand_id');
                $v['brand_title'] = db('pro_brands')->where('id',$brand_id)->value('brand_name');
            }
            if($v['feetype']){
                $v['feetype_title'] = db('cg_feiyongkemu3')->where('id',$v['feetype'])->value('title');
            }
            $v['yj_sjtrfy'] = $v['yjsjtrfy'];
            //$v['fb_hanshui'] = round($v['fb_shijiweishui']*(1+$v["fb_shuilv"]/100),2);
            $v['xsdcl'] = round($v['fb_shijixiaoliang']/$v['totalExpectedSales']*100,2);
            $v['sqfyl'] = round($v['yj_sjtrfy']/$v['totalExpectedSales']*100,2);
            $v['sjfyl'] = round($v['fb_shijitourufeiyong']/$v['fb_shijixiaoliang']*100,2);
             $v['gys_name']=db('gys')->where('id',$v['gys'])->value('gys_name');
             $v['jxs_title']=db('cg_jxs')->where('id',$v['jxs_id'])->value('title');
            $list[$k] = $v;
            $zj_yjws += $v['pizhunweishui'];
            $zj_yjhs += $v['pizhunhanshui'];
            $zj_yjtr += $v['yjsjtrfy'];
            $zj_sjws += $v['fb_shijiweishui'];
            $zj_sjhs += $v['fb_hanshui'];
            $zj_sjtr += $v['fb_shijitourufeiyong'];
            $zj_yjxl += $v['totalExpectedSales'];
            $zj_sjxl += $v['fb_shijixiaoliang'];
        }
        $zj_xsdcl = round($zj_sjxl/$zj_yjxl*100,2);
        $zj_sqfyl = round($zj_yjtr/$zj_yjxl*100,2);
        $zj_sjfyl = round($zj_sjtr/$zj_sjxl*100,2);
        $this->assign('list',$list);
        $this->assign('zj_yjws',$zj_yjws);
        $this->assign('zj_yjhs',$zj_yjhs);
        $this->assign('zj_yjtr',$zj_yjtr);
        $this->assign('zj_sjws',$zj_sjws);
        $this->assign('zj_sjhs',$zj_sjhs);
        $this->assign('zj_sjtr',$zj_sjtr);
        $this->assign('zj_yjxl',$zj_yjxl);
        $this->assign('zj_sjxl',$zj_sjxl);
        $this->assign('zj_xsdcl',$zj_xsdcl);
        $this->assign('zj_sqfyl',$zj_sqfyl);
        $this->assign('zj_sjfyl',$zj_sjfyl);
        $logs=$this->getlog(17,$id);
        $this->assign("logs",$logs);
        $btn=$this->getbton(17,$id);
        $this->assign("btn",$btn);
        return $this->fetch();
    }
    public function vtime($time){
        $output = '';
        foreach (array(86400 => '天', 3600 => '小时', 60 => '分', 1 => '秒') as $key => $value) {
            if ($time >= $key) $output .= floor($time/$key) . $value;
            $time %= $key;
        }
        return $output;
    }
    //审核明细
    public function shenhemx(){
        //$this->assign("from",$from);
        $workId = $this->request->param('workId');
        $trackId = $this->request->param('trackId');
        $myId = $this->request->param('myId');
        //yw_id
        $feedid =db('cg_workflow')->where('id',$myId)->value('yw_id');
        $id = $feedid;
        $data = Db::table('all_ti_feeback_list t')
            ->join('cg_ti t2','t2.id=t.apply_id')
            ->order("t.id desc")
            ->where('t.id',$id)
            ->field('t.*,t2.title,t2.cate_id,t2.type_id,t2.sn t2sn,t2.id t2id,t2.brief,t2.fb_brief,t2.jxs_id,t2.type_id')
            ->find();

        $data['cate_title'] = db('cg_ti_cate')->where('id',$data['cate_id'])->value('title');
        $data['cate_title2'] = db('cg_ti_type')->where('id',$data['type_id'])->value('title');
        $data['fankuiren'] = db('employee')->where('id',$data['c_user_id'])->value('em_name');
        $data['fankuirenjob'] = db('job t')->join('job_employee t2','t2.job_id=t.id')->where('employee_id',$data['c_user_id'])->value('t.job_name');
        $data['fankuirenbm'] = db('department t')->join('department_employee t2','t2.department_id=t.id')->where(['employee_id'=>$data['c_user_id'],'t2.index'=>1])->value('t.de_name');
        $principal = db('department t')->join('department_employee t2','t2.department_id=t.id')->where(['employee_id'=>$data['c_user_id'],'t2.index'=>1])->value('t.principal');
        $data['zhijieshangji'] = db('employee')->where('id',$principal)->value('em_name');
        $data['jxs_title'] = db('cg_jxs')->where('id',$data['jxs_id'])->value('title');
        $this->assign('data',$data);

        $list = db('cg_ti_list')->where('ti_id',$data['apply_id'])->select();
        $zj_yjws = 0;
        $zj_yjhs = 0;
        $zj_yjtr = 0;
        $zj_sjws = 0;
        $zj_sjhs = 0;
        $zj_sjtr = 0;
        $zj_yjxl = 0;
        $zj_sjxl = 0;
        foreach($list as $k=>$v){
            $v['brand_title'] = db('pro_brands')->where('id',$v['brand'])->value('brand_name');
            if($v['category']){
                $brand_id = db('pro_category')->where('id',$v['category'])->value('brand_id');
                $v['brand_title'] = db('pro_brands')->where('id',$brand_id)->value('brand_name');
            }
            if($v['series']){
                $category_id = db('pro_series')->where('id',$v['series'])->value('category_id');
                $brand_id = db('pro_category')->where('id',$category_id)->value('brand_id');
                $v['brand_title'] = db('pro_brands')->where('id',$brand_id)->value('brand_name');
            }
            if($v['sku']){
                $series_id = db('pro_sku')->where('id',$v['sku'])->value('series_id');
                $category_id = db('pro_series')->where('id',$series_id)->value('category_id');
                $brand_id = db('pro_category')->where('id',$category_id)->value('brand_id');
                $v['brand_title'] = db('pro_brands')->where('id',$brand_id)->value('brand_name');
            }
            if($v['product']){
                $sku_id = db('pro_products')->where('id',$v['product'])->value('sku_id');
                $series_id = db('pro_sku')->where('id',$sku_id)->value('series_id');
                $category_id = db('pro_series')->where('id',$series_id)->value('category_id');
                $brand_id = db('pro_category')->where('id',$category_id)->value('brand_id');
                $v['brand_title'] = db('pro_brands')->where('id',$brand_id)->value('brand_name');
            }
            if($v['tm_id']){
                $sku_id = db('pro_products')->where('id',$v['tm_id'])->value('sku_id');
                $series_id = db('pro_sku')->where('id',$sku_id)->value('series_id');
                $category_id = db('pro_series')->where('id',$series_id)->value('category_id');
                $brand_id = db('pro_category')->where('id',$category_id)->value('brand_id');
                $v['brand_title'] = db('pro_brands')->where('id',$brand_id)->value('brand_name');
            }
            if($v['feetype']){
                $v['feetype_title'] = db('cg_feiyongkemu3')->where('id',$v['feetype'])->value('title');
            }
            $v['yj_sjtrfy'] = $v['yjsjtrfy'];
            //$v['fb_hanshui'] = round($v['fb_shijiweishui']*(1+$v["fb_shuilv"]/100),2);
            $v['xsdcl'] = round($v['fb_shijixiaoliang']/$v['totalExpectedSales']*100,2);
            $v['sqfyl'] = round($v['yj_sjtrfy']/$v['totalExpectedSales']*100,2);
            $v['sjfyl'] = round($v['fb_shijitourufeiyong']/$v['fb_shijixiaoliang']*100,2);
             $v['gys_name']=db('gys')->where('id',$v['gys'])->value('gys_name');
             $v['jxs_title']=db('cg_jxs')->where('id',$v['jxs_id'])->value('title');
            $list[$k] = $v;
            $zj_yjws += $v['pizhunweishui'];
            $zj_yjhs += $v['pizhunhanshui'];
            $zj_yjtr += $v['yjsjtrfy'];
            $zj_sjws += $v['fb_shijiweishui'];
            $zj_sjhs += $v['fb_hanshui'];
            $zj_sjtr += $v['fb_shijitourufeiyong'];
            $zj_yjxl += $v['totalExpectedSales'];
            $zj_sjxl += $v['fb_shijixiaoliang'];
        }
        $zj_xsdcl = round($zj_sjxl/$zj_yjxl*100,2);
        $zj_sqfyl = round($zj_yjtr/$zj_yjxl*100,2);
        $zj_sjfyl = round($zj_sjtr/$zj_sjxl*100,2);
        $this->assign('list',$list);
        $this->assign('zj_yjws',$zj_yjws);
        $this->assign('zj_yjhs',$zj_yjhs);
        $this->assign('zj_yjtr',$zj_yjtr);
        $this->assign('zj_sjws',$zj_sjws);
        $this->assign('zj_sjhs',$zj_sjhs);
        $this->assign('zj_sjtr',$zj_sjtr);
        $this->assign('zj_yjxl',$zj_yjxl);
        $this->assign('zj_sjxl',$zj_sjxl);
        $this->assign('zj_xsdcl',$zj_xsdcl);
        $this->assign('zj_sqfyl',$zj_sqfyl);
        $this->assign('zj_sjfyl',$zj_sjfyl);
        $logs=$this->getlog(17,$id);
        $this->assign("logs",$logs);
        return $this->fetch();
    }

    //审核决策宝
    public function shenhe(){
        //$this->assign("from",$from);
         $workId = $this->request->param('workId');
        $trackId = $this->request->param('trackId');
        $myId = $this->request->param('myId');
        //yw_id
        $feedid =db('cg_workflow')->where('id',$myId)->value('yw_id');

        $this->assign('feedid',$feedid);
        $feed=db("all_ti_feeback_list")->where(["id"=>$feedid])->find();
        $feed["user"]=db("employee")->where(["id"=>$feed["c_user_id"]])->value("em_name");
        $feed["de_name"]=db("department_employee")->alias("de")->join("department d","d.id=de.department_id")->where("de.employee_id={$feed["c_user_id"]} and de.index=1")->value("de_name");
        $this->assign("feed",$feed);
        $yw_id=db("all_ti_feeback_list")->where(["id"=>$feedid])->value("apply_id");
        $state=db("all_ti_feeback_list")->where(["id"=>$feedid])->value("state");
        if($state==2){
             //打回
            $this->redirect('zuofei', ['id' => $feedid,"from"=>2]);
            return;
        }
         $info = db('cg_ti')->where('id',$yw_id)->find();
        $info['jxs_name'] = db('cg_jxs')->where('id',$info['jxs_id'])->value('title');
        $info['sqr'] = db('employee')->where('id',$info['cuser'])->value('em_name');
        $info['sqr_job'] = db('job t')->join('job_employee t2','t2.job_id=t.id')->where('t2.employee_id',$info['cuser'])->value('t.job_name');
        $info['zoning'] = db('cg_market_zoning t')->join('cg_jxs t2','t2.zoning=t.id')->where('t2.id',$info['jxs_id'])->value('t.title');
        $info['provice'] = db('cg_market_provice t')->join('cg_jxs t2','t2.provice=t.id')->where('t2.id',$info['jxs_id'])->value('t.title');
        $info['city'] = db('cg_market_city t')->join('cg_jxs t2','t2.city=t.id')->where('t2.id',$info['jxs_id'])->value('t.title');
        $info['jxs_type'] = db('cg_jxs_types t')->join('cg_jxs t2','t2.types=t.id')->where('t2.id',$info['jxs_id'])->value('t.title');
        $info['start'] = db('cg_ti_list')->where('ti_id',$yw_id)->value('start');
        $info['end'] = db('cg_ti_list')->where('ti_id',$yw_id)->value('end');
        $info['is_bu'] = db('cg_ti_list')->where('ti_id',$yw_id)->value('is_bu');
        $info["yujixl"]=round(db("cg_ti_list")->where('ti_id',$yw_id)->sum("totalExpectedSales"),2);
        $info["shijixl"]=round(db("cg_ti_list")->where('ti_id',$yw_id)->sum("fb_shijixiaoliang"),2);
        $info["xldc"]=round($info["shijixl"]/$info["yujixl"]*100,2);
        $info["yujihf"]=round(db("cg_ti_list")->where('ti_id',$yw_id)->sum("yjsjtrfy"),2);
        $info["shijihf"]=round(db("cg_ti_list")->where('ti_id',$yw_id)->sum("fb_shijitourufeiyong"),2);
        $info["hfdc"]=round($info["shijihf"]/$info["yujihf"]*100,2);

        $info["yujifyl"]=round($info["yujihf"]/$info["yujixl"]*100,2);
        if($info["shijixl"]!=0){
            $info["shijifyl"]=round($info["shijihf"]/$info["shijixl"]*100,2);
        }else{
            $info["shijifyl"]=0;
        }
        $info["fylc"]=$info["shijifyl"]-$info["yujifyl"];
        $netarr=Db::table("cg_ti_list")->where(["ti_id"=>$yw_id])->group("net_id")->field("sum(yjsjtrfy) as yjsjtrfy,sum(fb_shijitourufeiyong) as fb_shijitourufeiyong,sum(totalExpectedSales) as totalExpectedSales,sum(fb_shijixiaoliang) as fb_shijixiaoliang,net_id")->select();
        $info["you"]=0;$info["liang"]=0;$info["cha"]=0;$info["xiao"]=0;$info["da"]=0;
        for($i=0;$i<count($netarr);$i++){
            if($netarr[$i]["fb_shijixiaoliang"]>=$netarr[$i]["totalExpectedSales"]){
                $info["you"]++;
            }else if($netarr[$i]["fb_shijixiaoliang"]>=$netarr[$i]["totalExpectedSales"]*0.8){
                $info["liang"]++;
            }else{
                $info["cha"]++;
            }

            if($netarr[$i]["totalExpectedSales"]!=0&&$netarr[$i]["fb_shijixiaoliang"]!=0){
                if($netarr[$i]["fb_shijitourufeiyong"]/$netarr[$i]["fb_shijixiaoliang"]<=$netarr[$i]["yjsjtrfy"]/$netarr[$i]["totalExpectedSales"]){
                    $info["xiao"]++;
                }else{
                    $info["da"]++;
                }
            }elseif($netarr[$i]["totalExpectedSales"]==0&&$netarr[$i]["fb_shijixiaoliang"]!=0){
                $info["xiao"]++;
            }elseif($netarr[$i]["totalExpectedSales"]!=0&&$netarr[$i]["fb_shijixiaoliang"]!=0){
                $info["da"]++;
            }else{
                $info["xiao"]++;
            }
            $netarr[$i]["xldc"]=round($netarr[$i]["fb_shijixiaoliang"]/$netarr[$i]["totalExpectedSales"]*100,2);
            $netarr[$i]["fydc"]=round($netarr[$i]["fb_shijitourufeiyong"]/$netarr[$i]["yjsjtrfy"]*100,2);

            if(!empty($netarr[$i]["fb_shijixiaoliang"])){
                $netarr[$i]["fyl"]=round($netarr[$i]["fb_shijitourufeiyong"]/$netarr[$i]["fb_shijixiaoliang"]*100,2);
            }else{
                $netarr[$i]["fyl"]=0;
            }
            $netarr[$i]["fb_shijixiaoliang"]=round($netarr[$i]["fb_shijixiaoliang"],2);
            $netarr[$i]["fb_shijitourufeiyong"]=round($netarr[$i]["fb_shijitourufeiyong"],2);
            $netarr[$i]["net"]=Db::table("cg_net")->where(["id"=>$netarr[$i]["net_id"]])->value("title");
        }
        $this->assign("netarr",$netarr);
         $feiarr=Db::table("cg_ti_list")->where(["ti_id"=>$yw_id])->group("feetype")->field("sum(yjsjtrfy) as yjsjtrfy,sum(fb_shijitourufeiyong) as fb_shijitourufeiyong,sum(totalExpectedSales) as totalExpectedSales,sum(fb_shijixiaoliang) as fb_shijixiaoliang,feetype")->select();
         for($i=0;$i<count($feiarr);$i++){
            $feiarr[$i]["fee"]=Db::table("cg_feiyongkemu3")->where(["id"=>$feiarr[$i]["feetype"]])->value("title");
            $feiarr[$i]["fydc"]=round($feiarr[$i]["fb_shijitourufeiyong"]/$feiarr[$i]["yjsjtrfy"]*100,2);

             if(!empty($feiarr[$i]["fb_shijixiaoliang"])){
                 $feiarr[$i]["fyl"]=round($feiarr[$i]["fb_shijitourufeiyong"]/$feiarr[$i]["fb_shijixiaoliang"]*100,2);
             }else{
                 $feiarr[$i]["fyl"]=0;
             }


             $feiarr[$i]["fb_shijitourufeiyong"]=round($feiarr[$i]["fb_shijitourufeiyong"],2);
             $feiarr[$i]["net_num"]=Db::table("cg_ti_list")->where(["ti_id"=>$yw_id,"feetype"=>$feiarr[$i]["feetype"]])->group("net_id")->count();
             if($info['productlat']==1){
                $feiarr[$i]["p_num"]=Db::table("cg_ti_list")->where(["ti_id"=>$yw_id,"feetype"=>$feiarr[$i]["feetype"]])->group("brand")->count();
            }elseif($info['productlat']==2){
                $feiarr[$i]["p_num"]=Db::table("cg_ti_list")->where(["ti_id"=>$yw_id,"feetype"=>$feiarr[$i]["feetype"]])->group("category")->count();
            }elseif($info['productlat']==3){
                $feiarr[$i]["p_num"]=Db::table("cg_ti_list")->where(["ti_id"=>$yw_id,"feetype"=>$feiarr[$i]["feetype"]])->group("series")->count();
            }elseif($info['productlat']==4){
               $feiarr[$i]["p_num"]=Db::table("cg_ti_list")->where(["ti_id"=>$yw_id,"feetype"=>$feiarr[$i]["feetype"]])->group("sku")->count();
            }elseif($info['productlat']==5){
                $feiarr[$i]["p_num"]=Db::table("cg_ti_list")->where(["ti_id"=>$yw_id,"feetype"=>$feiarr[$i]["feetype"]])->group("product")->count();
            }
         }
         $this->assign("feiarr",$feiarr);
        if($info['productlat']==1){
            $brand_id = db('cg_ti_list')->where(['ti_id'=>$yw_id,'brand'=>['exp','is not null']])->value('brand');
            $info['pro_label'] = '品牌';
            $info['pro_name'] = db('pro_brands')->where('id',$brand_id)->value('brand_name');
            $info['p_cnt'] = db('cg_ti_list')->where('ti_id',$yw_id)->group("brand")->count('brand');
        }elseif($info['productlat']==2){
            $brand_id = db('cg_ti_list')->where(['ti_id'=>$yw_id,'category'=>['exp','is not null']])->value('category');
            $info['pro_label'] = '品类';
            $info['pro_name'] = db('pro_category')->where('id',$brand_id)->value('category_name');
            $info['p_cnt'] = db('cg_ti_list')->where('ti_id',$yw_id)->group("category")->count('category');
        }elseif($info['productlat']==3){
            $info['pro_label'] = '系列';
            $brand_id = db('cg_ti_list')->where(['ti_id'=>$yw_id,'series'=>['exp','is not null']])->value('series');
            $info['pro_name'] = db('pro_series')->where('id',$brand_id)->value('series_name');
            $info['p_cnt'] = db('cg_ti_list')->where('ti_id',$yw_id)->group("series")->count('series');
        }elseif($info['productlat']==4){
            $brand_id = db('cg_ti_list')->where(['ti_id'=>$yw_id,'sku'=>['exp','is not null']])->value('sku');
            $info['pro_label'] = 'sku';
            $info['pro_name'] = db('pro_sku')->where('id',$brand_id)->value('sku_name');
            $info['p_cnt'] = db('cg_ti_list')->where('ti_id',$yw_id)->group("sku")->count('sku');
        }elseif($info['productlat']==5){
            $brand_id = db('cg_ti_list')->where(['ti_id'=>$yw_id,'product'=>['exp','is not null']])->value('product');
            $info['pro_label'] = '产品';
            $info['pro_name'] = db('pro_products')->where('id',$brand_id)->value('pro_name');
            $info['p_cnt'] = db('cg_ti_list')->where('ti_id',$yw_id)->group("product")->count('product');
        }



        $apply=Db::table("cg_ti")->where(["id"=>$yw_id])->find();
        $net_num = Db::table("cg_ti_list")->where(["ti_id"=>$yw_id])->group("net_id")->count();
        $this->assign("net_num", $net_num);
        $info["xitong"]=Db::table("cg_ti")->alias("ap")
            ->join("cg_ti_list apd", "apd.ti_id=ap.id")
            ->join("cg_net n", "apd.net_id=n.id", "LEFT")
            ->join("cg_system s", "n.system_id=s.id", "LEFT")->where(["ap.id" => $yw_id])->value("s.title");
     
        $info["xitongtype"]=Db::table("cg_ti")->alias("ap")
            ->join("cg_ti_list apd", "apd.ti_id=ap.id")
            ->join("cg_net n", "apd.net_id=n.id", "LEFT")
            ->join("cg_system s", "n.system_id=s.id", "LEFT")->join("cg_customertype t","s.cusType=t.id")->where(["ap.id" => $yw_id])->value("t.title");
             $system_num=Db::table("cg_ti")->alias("ap")
            ->join("cg_ti_list apd", "apd.ti_id=ap.id")
            ->join("cg_net n", "apd.net_id=n.id", "LEFT")
            ->join("cg_system s", "n.system_id=s.id", "LEFT")->where(["ap.id" => $yw_id])->group("s.id")->count();
            $this->assign("system_num",$system_num);
        $info["net"]=Db::table("cg_ti")->alias("ap")
            ->join("cg_ti_list apd", "apd.ti_id=ap.id")
            ->join("cg_net n", "apd.net_id=n.id", "LEFT")->where(["ap.id" => $yw_id])->value("n.title");
        $info["nettype"]=Db::table("cg_ti")->alias("ap")
            ->join("cg_ti_list apd", "apd.ti_id=ap.id")
            ->join("cg_net n", "apd.net_id=n.id", "LEFT")->join("cg_networktype cn","n.cat=cn.id")->where(["ap.id" => $yw_id])->value("cn.title");

         //本次申请已耗时
        $time = time()-$info['ctime'];
        $bcsqhs = $this->vtime($time);
        $this->assign('bcsqhs',$bcsqhs);

        //获取操作记录
        $post_data = [];
        $post_data['accessToken'] = session('accessToken');
        $post_data['workId'] = $workId;
        $czjl = $this->curl_post(config('workflow_url').'/horizon/workflow/rest/flow/support/history.wf',$post_data);
        $czjl = object_array($czjl);
//        echo "<pre>";print_r($czjl);
        foreach($czjl as $k=>$v){
            //查询审批意见
            $wf_id = 0;
//            $v['spyj'] = db('cg_workflow_spyj')->where(['wf_id'=>$wf_id,'spr_id']);
        }
        $this->assign('czjl',$czjl);

        //查询附件
        $attachs = db('cg_workflow_spyj')->where('wf_id',$myId)->column('attach');
        if($attachs){
            $attach = [];
            foreach($attachs as $item){
                if($item){
                    $item = unserialize($item);
                    foreach($item as $k=>$v){
                        $temp = Db::table('annex')->where(array('id'=>$v))->find();
                        $temp['path'] = '/uploads/'.$temp['path'];
                        $item[$k] = $temp;
                    }
                    $attach = array_merge($attach,$item);
                }
            }

            $this->assign('attach',$attach);

        }
         //费用类型
        $info['fylx'] = Db::query('select count(*) cnt from (select feetype from cg_ti_list where ti_id='.$yw_id.' group by feetype) t')[0]['cnt'];
        $info['fylx_name'] = db('cg_ti_list t')->join('cg_feiyongkemu3 t2','t2.id=t.feetype')->where('ti_id',$yw_id)->value('t2.title');
        $this->assign("info",$info);
        return $this->fetch();
    }

    //提交工作流
    public function tijiao($id){
        $jieguo=$this->gowork(17,$id);
                if($jieguo==json_encode(['state'=>1,'msg'=>'提交成功'])){
                    Db::commit();
                    return $jieguo;
                }else{
                    Db::rollBack();
                    return json_encode(["msg"=>"系统繁忙请稍后再试","state"=>2]);
                }
    }
    public function zuofeichuli(){
        $request = Request::instance();$data = $request->post();
        //print_r($data);exit;
        $fid=17;$aid=$data["aid"];$id=$aid;
        
            $res=$this->goworker($fid,$aid,$data["nodeKey"],$data["selectId"],$data["selectName"]);
           // print_r($res);exit;
            if($res["operateStatus"]=="success"){
                    $ti_id=db("all_ti_feeback_list")->where(["id"=>$id])->value("apply_id");
                    Db::startTrans();
                    try{
                        Db::table("cg_ti")->where(["id"=>$ti_id])->update(["fb"=>0,"fd_id"=>null]);
                        Db::table("all_ti_feeback_list")->where(["id"=>$id])->update(["is_deleted"=>0,"state"=>8]);
                        Db::table("cg_ti_list")->where(["ti_id"=>$ti_id])->update(["fb_shuilv"=>null,"fb_isVAT"=>null,"fb_shijixiaoliang"=>null,"fb_brief"=>null,"fb_shijiweishui"=>null,"fb_hanshui"=>null,"fb_shijitourufeiyong"=>null,"fb_time"=>null,"fb_user"=>null,"fb_boxnum"=>null,"hx_status"=>3]);
                    }catch (\Exception $e) {
                            //echo $e->getMessage();
                            Db::rollBack();
                            return json_encode(["msg" => $e->getMessage(), "state" => 2]);
                    }
                    Db::commit();
                    return json_encode(["msg" => "", "state" => 1]);
                }else{
                return json_encode(["msg"=>"系统繁忙请稍后再试","state"=>2]);
                }
      
        
    }
    //取消
    public function qx($id){
        $ti_id=db("all_ti_feeback_list")->where(["id"=>$id])->value("apply_id");
        Db::startTrans();
        try{
            Db::table("cg_ti")->where(["id"=>$ti_id])->update(["fb"=>0,"fd_id"=>null]);
            Db::table("all_ti_feeback_list")->where(["id"=>$id])->update(["is_deleted"=>1]);
            Db::table("cg_ti_list")->where(["ti_id"=>$ti_id])->update(["fb_shuilv"=>null,"fb_isVAT"=>null,"fb_shijixiaoliang"=>null,"fb_brief"=>null,"fb_shijiweishui"=>null,"fb_hanshui"=>null,"fb_shijitourufeiyong"=>null,"fb_time"=>null,"fb_user"=>null,"fb_boxnum"=>null,"hx_status"=>3]);
        }catch (\Exception $e) {
                //echo $e->getMessage();
                Db::rollBack();
                return json_encode(["msg" => $e->getMessage(), "state" => 2]);
        }
        Db::commit();
        return json_encode(["msg" => "", "state" => 1]);
        
    }
    //已反馈列表
    public function index2($page=1,$list=20){
        $request = Request::instance();


        $data = $request->post();
        $eid=Session::get("employee_id");
        $where="t.is_deleted=0 and t.is_actived=1 and t.c_user_id=$eid ";
         if ($data) {
            if ($data["no"])
                $where .= "and t.no={$data["no"]} ";
            if (trim($data["title"]))
                $where .= "and t2.title like '%{$data["title"]}%' ";
            if ($data["status"])
                $where .= "and t.state={$data["status"]} ";
            if (trim($data["start"])) {
                //$st = strtotime($data["start"]);
                $where .= "and t.ctime>='{$data["start"]}' ";
            }
            if (trim($data["end"])) {
                //$ed = strtotime($data["end"]) + 24 * 3600;
                $where .= "and t.ctime<='{$data["end"]}' ";
            }
        } else {
            $data = ["no" => "", "title" => "", "status" => "", "start" => "", "end" => ""];
        }

        $sum = Db::table('all_ti_feeback_list t')
            ->join('cg_ti t2','t2.id=t.apply_id')
            ->where($where)->count();
            $maxpage=ceil($sum/$list);
            $page=$page<$maxpage?$page:$maxpage;
        $page=$page>1?$page:1;

        $start=($page-1)*$list;
        $applys = Db::table('all_ti_feeback_list t')
            ->join('cg_ti t2','t2.id=t.apply_id')
            ->join("ti_state ts","ts.id=t.state")
            ->order("t.id desc")
            ->where($where)->limit($start,$list)
            ->field('t.*,t2.title,t2.cate_id,ts.title as statet')
            ->select();


        foreach($applys as $k=>$v){
            $applys[$k]['c_user_id'] = Db::table('employee')->where(array('id'=>$v['c_user_id']))->value('em_name');
            $applys[$k]['cate_title'] = Db::table('cg_ti_cate')->where(array('id'=>$v['cate_id']))->value('title');
            $applys[$k]["myId"]=Db::table("cg_workflow")->where(["yw_id"=>$v["id"],"ywlx"=>17])->value("id");
        }

        $this->assign("applys", $applys);

        $this->assign("data", $data);
        $this->assign("sum",$sum);
        $this->assign("page",$page);
        $this->assign("list",$list);
        $this->assign("maxpage",$maxpage);

        return $this->fetch();
    }
	//反馈列表
    public function index($page=1,$list=20)
    {

         $request = Request::instance();


        $data = $request->post(); $eid=Session::get("employee_id");
        $where = "ap.status=7 and ap.fb=0 and ap.sfxyfk=1 and ap.status2 is null and ap.cuser=$eid ";
        if ($data) {
            if ($data["no"])
                $where .= "and ap.sn={$data["no"]} ";
            if (trim($data["title"]))
                $where .= "and ap.title like '%{$data["title"]}%' ";
            
            if (trim($data["start"])) {
                $st = strtotime($data["start"]);
                $where .= "and ap.ctime>=$st ";
            }
            if (trim($data["end"])) {
                $ed = strtotime($data["end"]) + 24 * 3600;
                $where .= "and ap.ctime<$ed ";
            }
        } else {
            $data = ["no" => "", "title" => "", "start" => "", "end" => ""];
        }

     $sum=Db::table("cg_ti")->alias("ap")
            ->join("cg_ti yuan", "ap.old=yuan.id", "LEFT")
            ->join("cg_ti_cate cc", "ap.cate_id=cc.id")
            ->join("employee ec", "ap.cuser=ec.id", "LEFT")
            ->where($where)->count();
             $maxpage=ceil($sum/$list);
        $page=$page<$maxpage?$page:$maxpage;
        $page=$page>1?$page:1;

        $start=($page-1)*$list;
        $applys = Db::table("cg_ti")->alias("ap")
            ->join("cg_ti yuan", "ap.old=yuan.id", "LEFT")
            ->join("cg_ti_cate cc", "ap.cate_id=cc.id")
            ->join("employee ec", "ap.cuser=ec.id", "LEFT")
            ->where($where)
            ->order("ap.ctime desc,ap.id desc")->limit($start,$list)
            ->field("ap.id,ap.sn,ap.title,ap.status,ap.status2,ec.em_name as cname,ap.ctime,ap.fb,ap.cate_id,ap.sfxyfk,ap.type_id,ap.arealat")
            ->select();
        for ($i = 0; $i < count($applys); $i++) {
            switch ($applys[$i]["status"]) {

                case 2:
                    $applys[$i]["status_type"] = "已打回";
                    break;
                case 3:
                    $applys[$i]["status_type"] = "区域领导审批";
                    break;
                case 4:
                    $applys[$i]["status_type"] = "拆分人审批";
                    break;
                case 5:
                    $applys[$i]["status_type"] = "集团领导审批";
                    break;
                case 6:
                    $applys[$i]["status_type"] = "抄送";
                    break;
                case 7:
                    $applys[$i]["status_type"] = "已归档";
                    break;
                case 8:
                    $applys[$i]["status_type"] = "已作废";
                    break;
                case 9:
                    $applys[$i]["status_type"] = "已否决";
                    break;
                default:
                    $applys[$i]["status_type"] = "草稿";
                    break;
            }
             $applys[$i]["ctime"] = date("Y-m-d", $applys[$i]["ctime"]);
         }

        $this->assign("applys", $applys);

        $this->assign("data", $data);
        $this->assign("sum",$sum);
        $this->assign("page",$page);
        $this->assign("list",$list);
        $this->assign("maxpage",$maxpage);

        return $this->fetch();
    }

    /**
     * 反馈提交工作流
     * @throws \think\Exception
     */
    public function submitWorkflow(){
        $id = $this->request->param('id');
        if(!$id){
            echo  json_encode([
                "success"=>false,
                "msg"=>"参数错误"
            ]);die;
        }

        $bool = Db::table('all_ti_feeback_list')
            ->where(array('id'=>$id))
            ->update([
                'state'=>1
            ]);
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"提交成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"提交失败"
            ]);die;
        }
    }

    //反馈详情页
    public function view(){
        $id = $this->request->param('id');
        $data = Db::table('all_ti_feeback_list t')
            ->join('cg_ti t2','t2.id=t.apply_id')
            ->order("t.id desc")
            ->where('t.id',$id)
            ->field('t.*,t2.title,t2.cate_id,t2.type_id,t2.sn t2sn,t2.id t2id,t2.brief,t2.fb_brief,t2.jxs_id,t2.type_id')
            ->find();

        $data['cate_title'] = db('cg_ti_cate')->where('id',$data['cate_id'])->value('title');
        $data['cate_title2'] = db('cg_ti_type')->where('id',$data['type_id'])->value('title');
        $data['fankuiren'] = db('employee')->where('id',$data['c_user_id'])->value('em_name');
        $data['fankuirenjob'] = db('job t')->join('job_employee t2','t2.job_id=t.id')->where('employee_id',$data['c_user_id'])->value('t.job_name');
        $data['fankuirenbm'] = db('department t')->join('department_employee t2','t2.department_id=t.id')->where(['employee_id'=>$data['c_user_id'],'t2.index'=>1])->value('t.de_name');
        $principal = db('department t')->join('department_employee t2','t2.department_id=t.id')->where(['employee_id'=>$data['c_user_id'],'t2.index'=>1])->value('t.principal');
        $data['zhijieshangji'] = db('employee')->where('id',$principal)->value('em_name');
        $data['jxs_title'] = db('cg_jxs')->where('id',$data['jxs_id'])->value('title');
        $this->assign('data',$data);

        $list = db('cg_ti_list')->where('ti_id',$data['apply_id'])->select();
        $zj_yjws = 0;
        $zj_yjhs = 0;
        $zj_yjtr = 0;
        $zj_sjws = 0;
        $zj_sjhs = 0;
        $zj_sjtr = 0;
        $zj_yjxl = 0;
        $zj_sjxl = 0;
        foreach($list as $k=>$v){
            $v['brand_title'] = db('pro_brands')->where('id',$v['brand'])->value('brand_name');
            if($v['category']){
                $brand_id = db('pro_category')->where('id',$v['category'])->value('brand_id');
                $v['brand_title'] = db('pro_brands')->where('id',$brand_id)->value('brand_name');
            }
            if($v['series']){
                $category_id = db('pro_series')->where('id',$v['series'])->value('category_id');
                $brand_id = db('pro_category')->where('id',$category_id)->value('brand_id');
                $v['brand_title'] = db('pro_brands')->where('id',$brand_id)->value('brand_name');
            }
            if($v['sku']){
                $series_id = db('pro_sku')->where('id',$v['sku'])->value('series_id');
                $category_id = db('pro_series')->where('id',$series_id)->value('category_id');
                $brand_id = db('pro_category')->where('id',$category_id)->value('brand_id');
                $v['brand_title'] = db('pro_brands')->where('id',$brand_id)->value('brand_name');
            }
            if($v['product']){
                $sku_id = db('pro_products')->where('id',$v['product'])->value('sku_id');
                $series_id = db('pro_sku')->where('id',$sku_id)->value('series_id');
                $category_id = db('pro_series')->where('id',$series_id)->value('category_id');
                $brand_id = db('pro_category')->where('id',$category_id)->value('brand_id');
                $v['brand_title'] = db('pro_brands')->where('id',$brand_id)->value('brand_name');
            }
            if($v['tm_id']){
                $sku_id = db('pro_products')->where('id',$v['tm_id'])->value('sku_id');
                $series_id = db('pro_sku')->where('id',$sku_id)->value('series_id');
                $category_id = db('pro_series')->where('id',$series_id)->value('category_id');
                $brand_id = db('pro_category')->where('id',$category_id)->value('brand_id');
                $v['brand_title'] = db('pro_brands')->where('id',$brand_id)->value('brand_name');
            }
            if($v['feetype']){
                $v['feetype_title'] = db('cg_feiyongkemu3')->where('id',$v['feetype'])->value('title');
            }
            $v['yj_sjtrfy'] = $v['yjsjtrfy'];
            //$v['fb_hanshui'] = round($v['fb_shijiweishui']*(1+$v["fb_shuilv"]/100),2);
            $v['xsdcl'] = round($v['fb_shijixiaoliang']/$v['totalExpectedSales']*100,2);
            $v['sqfyl'] = round($v['yj_sjtrfy']/$v['totalExpectedSales']*100,2);
            $v['sjfyl'] = round($v['fb_shijitourufeiyong']/$v['fb_shijixiaoliang']*100,2);
             $v['gys_name']=db('gys')->where('id',$v['gys'])->value('gys_name');
             $v['jxs_title']=db('cg_jxs')->where('id',$v['jxs_id'])->value('title');
            $list[$k] = $v;
            $zj_yjws += $v['pizhunweishui'];
            $zj_yjhs += $v['pizhunhanshui'];
            $zj_yjtr += $v['yjsjtrfy'];
            $zj_sjws += $v['fb_shijiweishui'];
            $zj_sjhs += $v['fb_hanshui'];
            $zj_sjtr += $v['fb_shijitourufeiyong'];
            $zj_yjxl += $v['totalExpectedSales'];
            $zj_sjxl += $v['fb_shijixiaoliang'];
        }
        $zj_xsdcl = round($zj_sjxl/$zj_yjxl*100,2);
        $zj_sqfyl = round($zj_yjtr/$zj_yjxl*100,2);
        $zj_sjfyl = round($zj_sjtr/$zj_sjxl*100,2);
        $this->assign('list',$list);
        $this->assign('zj_yjws',$zj_yjws);
        $this->assign('zj_yjhs',$zj_yjhs);
        $this->assign('zj_yjtr',$zj_yjtr);
        $this->assign('zj_sjws',$zj_sjws);
        $this->assign('zj_sjhs',$zj_sjhs);
        $this->assign('zj_sjtr',$zj_sjtr);
        $this->assign('zj_yjxl',$zj_yjxl);
        $this->assign('zj_sjxl',$zj_sjxl);
        $this->assign('zj_xsdcl',$zj_xsdcl);
        $this->assign('zj_sqfyl',$zj_sqfyl);
        $this->assign('zj_sjfyl',$zj_sjfyl);
        $logs=$this->getlog(17,$id);
        $this->assign("logs",$logs);
        return $this->fetch();
    }

    //导出excel
    public function exportview(){
        //获取数据
        $id = $this->request->param('id');
        $data = Db::table('all_ti_feeback_list t')
            ->join('cg_ti t2','t2.id=t.apply_id')
            ->order("t.id desc")
            ->where('t.id',$id)
            ->field('t.*,t2.title,t2.cate_id,t2.type_id,t2.sn t2sn,t2.id t2id,t2.brief,t2.fb_brief,t2.jxs_id')
            ->find();
        $data['cate_title'] = db('cg_ti_cate')->where('id',$data['cate_id'])->value('title');
        $data['cate_title2'] = db('cg_ti_type')->where('id',$data['type_id'])->value('title');
        $data['fankuiren'] = db('employee')->where('id',$data['c_user_id'])->value('em_name');
        $data['fankuirenjob'] = db('job t')->join('job_employee t2','t2.job_id=t.id')->where('employee_id',$data['c_user_id'])->value('t.job_name');
        $data['fankuirenbm'] = db('department t')->join('department_employee t2','t2.department_id=t.id')->where(['employee_id'=>$data['c_user_id'],'t2.index'=>1])->value('t.de_name');
        $principal = db('department t')->join('department_employee t2','t2.department_id=t.id')->where(['employee_id'=>$data['c_user_id'],'t2.index'=>1])->value('t.principal');
        $data['zhijieshangji'] = db('employee')->where('id',$principal)->value('em_name');
        $data['jxs_title'] = db('cg_jxs')->where('id',$data['jxs_id'])->value('title');

        $list = db('cg_ti_list')->where('ti_id',$data['apply_id'])->select();
        $zj_yjws = 0;
        $zj_yjhs = 0;
        $zj_yjtr = 0;
        $zj_sjws = 0;
        $zj_sjhs = 0;
        $zj_sjtr = 0;
        $zj_yjxl = 0;
        $zj_sjxl = 0;
        foreach($list as $k=>$v){
            $v['brand_title'] = db('pro_brands')->where('id',$v['brand'])->value('brand_name');
            if($v['category']){
                $brand_id = db('pro_category')->where('id',$v['category'])->value('brand_id');
                $v['brand_title'] = db('pro_brands')->where('id',$brand_id)->value('brand_name');
            }
            if($v['series']){
                $category_id = db('pro_series')->where('id',$v['series'])->value('category_id');
                $brand_id = db('pro_category')->where('id',$category_id)->value('brand_id');
                $v['brand_title'] = db('pro_brands')->where('id',$brand_id)->value('brand_name');
            }
            if($v['sku']){
                $series_id = db('pro_sku')->where('id',$v['sku'])->value('series_id');
                $category_id = db('pro_series')->where('id',$series_id)->value('category_id');
                $brand_id = db('pro_category')->where('id',$category_id)->value('brand_id');
                $v['brand_title'] = db('pro_brands')->where('id',$brand_id)->value('brand_name');
            }
            if($v['product']){
                $sku_id = db('pro_products')->where('id',$v['product'])->value('sku_id');
                $series_id = db('pro_sku')->where('id',$sku_id)->value('series_id');
                $category_id = db('pro_series')->where('id',$series_id)->value('category_id');
                $brand_id = db('pro_category')->where('id',$category_id)->value('brand_id');
                $v['brand_title'] = db('pro_brands')->where('id',$brand_id)->value('brand_name');
            }
            if($v['tm_id']){
                $sku_id = db('pro_products')->where('id',$v['tm_id'])->value('sku_id');
                $series_id = db('pro_sku')->where('id',$sku_id)->value('series_id');
                $category_id = db('pro_series')->where('id',$series_id)->value('category_id');
                $brand_id = db('pro_category')->where('id',$category_id)->value('brand_id');
                $v['brand_title'] = db('pro_brands')->where('id',$brand_id)->value('brand_name');
            }
            if($v['feetype']){
                $v['feetype_title'] = db('cg_feiyongkemu3')->where('id',$v['feetype'])->value('title');
            }
            $v['yj_sjtrfy'] = $v['isVAT']?$v['amount']:$v['hanshui'];
            $v['fb_hanshui'] = round($v['fb_shijiweishui']*(1+$v["fb_shuilv"]/100),2);
            $v['xsdcl'] = round($v['fb_shijixiaoliang']/$v['totalExpectedSales']*100,2);
            $v['sqfyl'] = round($v['yj_sjtrfy']/$v['totalExpectedSales']*100,2);
            $v['sjfyl'] = round($v['fb_shijitourufeiyong']/$v['fb_shijixiaoliang']*100,2);
            $list[$k] = $v;
            $zj_yjws += $v['amount'];
            $zj_yjhs += $v['hanshui'];
            $zj_yjtr += $v['yj_sjtrfy'];
            $zj_sjws += $v['fb_shijiweishui'];
            $zj_sjhs += $v['fb_hanshui'];
            $zj_sjtr += $v['fb_shijitourufeiyong'];
            $zj_yjxl += $v['totalExpectedSales'];
            $zj_sjxl += $v['fb_shijixiaoliang'];
        }
        $zj_xsdcl = round($zj_sjxl/$zj_yjxl*100,2);
        $zj_sqfyl = round($zj_yjtr/$zj_yjxl*100,2);
        $zj_sjfyl = round($zj_sjtr/$zj_sjxl*100,2);

        Vendor("PHPExcel");
        Vendor("PHPExcel.Writer.Excel5");

        $objPHPExcel= new PHPExcel();
        $objProps=$objPHPExcel->getProperties();

        $objProps->setCreator('admin');
        $objProps->setLastModifiedBy('admin');
        $objProps->setTitle('Office 2003 XLSX Test Document');
        $objProps->setSubject('Office 2003 XLSX Test Document');
        $objProps->setDescription("Test document, generated by PHPExcel.");
        $objProps->setKeywords("office excel PHPExcel");
        $objProps->setCategory("Test");

        $objPHPExcel->setActiveSheetIndex(0);
        $objPHPExcel->getActiveSheet()->setTitle("非常规促销反馈表");

        #################总标题
        $styleArray = array(
            'borders' => array(
                'allborders' => array(
                    //'style' => PHPExcel_Style_Border::BORDER_THICK,//边框是粗的
                    'style' => PHPExcel_Style_Border::BORDER_THIN,//细边框
                    'color' => array('argb' => '000000'),
                ),
            ),
        );
        $n = $objPHPExcel->getActiveSheet()->getHighestRow();
        $s = $objPHPExcel->getActiveSheet();
        $s->getStyle('A1:AC'.$n)->applyFromArray($styleArray);
        $s->getRowDimension('1')->setRowHeight(20);
        $s->getRowDimension('2')->setRowHeight(20);
        $s->getRowDimension('3')->setRowHeight(20);
        $s->getRowDimension('4')->setRowHeight(30);
        $s->mergeCells('A1:AC1');
        $s->setCellValue('A1',"非常规促销反馈表");
        //水平剧中
        $s->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        //垂直剧中
        $s->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
        //加粗
        $s->getStyle('A1')->getFont()->setBold(true);
        //字体
        $s->getStyle('A1')->getFont()->setSize(14);
        #################表头信息
        $s->mergeCells('A2:AC2');
        $s->setCellValue('A2',"(本表单由软件导出，导出时间：".date("Y-m-d H:i:s")."；导出人：{".db('employee')->where('id',session('employee_id'))->value('em_name')."})");
        //水平剧中
        $s->getStyle('A2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        //垂直剧中
        $s->getStyle('A2')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
        //加粗
        $s->getStyle('A2')->getFont()->setBold(true);
        //字体
        $s->getStyle('A2')->getFont()->setSize(11);

        $s->mergeCells('A3:B3');
        $s->setCellValue('A3','主题');
        $s->mergeCells('C3:J3');
        $s->setCellValue('C3',$data['title']);
        $s->mergeCells('K3:L3');
        $s->setCellValue('K3','活动类型');
        $s->mergeCells('M3:P3');
        $s->setCellValue('M3','非常规促销');
        $s->mergeCells('A4:B4');
        $s->setCellValue('A4','编号');
        $s->mergeCells('C4:E4');
        $s->setCellValue('C4',$data['no']);
        $s->mergeCells('F4:G4');
        $s->setCellValue('F4','日期');
        $s->mergeCells('H4:J4');
        $s->setCellValue('H4',$data['created_on']);
        $s->mergeCells('K4:L4');
        $s->setCellValue('K4','原申请编号');
        $s->mergeCells('M4:P4');
        $s->setCellValue('M4',$data['t2sn']);
        $s->mergeCells('A5:B5');
        $s->setCellValue('A5','反馈人');
        $s->mergeCells('C5:E5');
        $s->setCellValue('C5',$data['fankuiren'].'('.$data['fankuirenjob'].')');
        $s->mergeCells('F5:G5');
        $s->setCellValue('F5','所属部门');
        $s->mergeCells('H5:J5');
        $s->setCellValue('H5',$data['fankuirenbm']);
        $s->mergeCells('K5:L5');
        $s->setCellValue('K5','直属上级');
        $s->mergeCells('M5:P5');
        $s->setCellValue('M5',$data['zhijieshangji']);
        $s->mergeCells('A6:P6');
        $s->setCellValue('A6',$data['brief']);
        $s->mergeCells('A7:A8');
        $s->setCellValue('A7','审批人');
        $s->mergeCells('B7:B8');
        $s->setCellValue('B7','序号');
        $s->mergeCells('C7:C8');
        $s->setCellValue('C7','明细编号');
        $s->mergeCells('D7:D8');
        $s->setCellValue('D7','原明细编号');
        $s->mergeCells('E7:E8');
        $s->setCellValue('E7','经销商');
        $s->mergeCells('F7:F8');
        $s->setCellValue('F7','网点');
        $s->mergeCells('G7:G8');
        $s->setCellValue('G7','品牌');
        $s->mergeCells('H7:H8');
        $s->setCellValue('H7','产品系列');
        $s->mergeCells('I7:I8');
        $s->setCellValue('I7','单品');
        $s->mergeCells('J7:J8');
        $s->setCellValue('J7','活动日期');
        $s->mergeCells('K7:K8');
        $s->setCellValue('K7','三级费用科目');
        $s->mergeCells('L7:L8');
        $s->setCellValue('L7','收款方');
        $s->mergeCells('M7:Q7');
        $s->setCellValue('M7','预计花费');
        //水平剧中
        $s->getStyle('M7')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        //垂直剧中
        $s->getStyle('M7')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $s->setCellValue('M8','预计未税金额（元）');
        $s->setCellValue('N8','是否增值税（可抵扣）');
        $s->setCellValue('O8','税率');
        $s->setCellValue('P8','预计含税金额（元）');
        $s->setCellValue('Q8','预计投入费用（元）');
        $s->mergeCells('R7:V7');
        $s->setCellValue('R7','实际花费');
        $s->setCellValue('R8','实际未税金额（元）');
        $s->setCellValue('S8','是否增值税（可抵扣）');
        $s->setCellValue('T8','税率');
        $s->setCellValue('U8','实际含税金额（元）');
        $s->setCellValue('V8','实际投入费用（元）');
        $s->mergeCells('W7:W8');
        $s->setCellValue('W7','预计销量（元）');
        $s->mergeCells('X7:X8');
        $s->setCellValue('X7','实际销量（元）');
        $s->mergeCells('Y7:Y8');
        $s->setCellValue('Y7','销售达成率');
        $s->mergeCells('Z7:Z8');
        $s->setCellValue('Z7','申请费用率');
        $s->mergeCells('AA7:AA8');
        $s->setCellValue('AA7','实际费用率');
        $s->mergeCells('AB7:AB8');
        $s->setCellValue('AB7','活动描述');
        $s->mergeCells('AC7:AC8');
        $s->setCellValue('AC7','负责人');
        $s->getStyle('A7:AC8')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
        $s->getStyle('A7:AC8')->getFill()->getStartColor()->setARGB('0099CCFF');

        foreach($list as $k=>$v){
            $c = $k+9;
            $s->setCellValue('A'.$c,'无');
            $s->setCellValue('B'.$c,$k+1);
            $s->setCellValue('C'.$c,$data['t2sn'].'-'.$v['sn']);
            $s->setCellValue('D'.$c,$data['t2sn'].'-'.$v['sn']);
            $s->setCellValue('E'.$c,$data['jxs_title']);
            $s->setCellValue('F'.$c,'');
            $s->setCellValue('G'.$c,$v['brand_title']);
            $s->setCellValue('H'.$c,'');
            $s->setCellValue('I'.$c,'');
            $s->setCellValue('J'.$c,date('Y-m-d',$v['start']).'至'.date('Y-m-d',$v['end']));
            $s->setCellValue('K'.$c,$v['feetype_title']);
            $s->setCellValue('L'.$c,'');
            $s->setCellValue('M'.$c,$v['amount']);
            $s->setCellValue('N'.$c,$v['isVAT']==1?'是':'否');
            $s->setCellValue('O'.$c,$v['shuilv'].'%');
            $s->setCellValue('P'.$c,$v['hanshui']);
            $s->setCellValue('Q'.$c,$v['yj_sjtrfy']);
            $s->setCellValue('R'.$c,$v['fb_shijiweishui']);
            $s->setCellValue('S'.$c,$v['fb_isVAT']==1?'是':'否');
            $s->setCellValue('T'.$c,$v['fb_shuilv'].'%');
            $s->setCellValue('U'.$c,$v['fb_hanshui']);
            $s->setCellValue('V'.$c,$v['fb_shijitourufeiyong']);
            $s->setCellValue('W'.$c,$v['totalExpectedSales']);
            $s->setCellValue('X'.$c,$v['fb_shijixiaoliang']);
            $s->setCellValue('Y'.$c,$v['xsdcl'].'%');
            $s->setCellValue('Z'.$c,$v['sqfyl'].'%');
            $s->setCellValue('Z'.$c,$v['sqfyl'].'%');
            $s->setCellValue('AA'.$c,$v['sjfyl'].'%');
            $s->setCellValue('AB'.$c,$v['brief'].'%');
            $s->setCellValue('AC'.$c,$v['head'].'%');
        }
        $c = 9+count($list);
        $s->mergeCells('A'.$c.":L".$c);
        $s->getStyle('A'.$c)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        $s->setCellValue('A'.$c,'合计');
        $s->setCellValue('M'.$c,$zj_yjws);
        $s->setCellValue('P'.$c,$zj_yjhs);
        $s->setCellValue('Q'.$c,$zj_yjtr);
        $s->setCellValue('R'.$c,$zj_sjws);
        $s->setCellValue('U'.$c,$zj_sjhs);
        $s->setCellValue('V'.$c,$zj_sjtr);
        $s->setCellValue('W'.$c,$zj_yjxl);
        $s->setCellValue('X'.$c,$zj_sjxl);
        $s->setCellValue('Y'.$c,$zj_xsdcl.'%');
        $s->setCellValue('Z'.$c,$zj_sqfyl.'%');
        $s->setCellValue('AA'.$c,$zj_sjfyl.'%');

        $c2 = $c+1;
        $s->mergeCells('A'.$c2.":P".$c2);
        $s->setCellValue('A'.$c2,'备注：'.$data['fb_brief']);

        $c3 = $c2+2;
        $s->mergeCells('A'.$c3.":P".$c3);
        $s->getStyle('A'.$c3.":P".$c3)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
        $s->getStyle('A'.$c3.":P".$c3)->getFill()->getStartColor()->setARGB('0099CCFF');
        $s->setCellValue('A'.$c3,"审批意见");
        $logs=$this->getlog(17,$id);
        $this->assign("logs",$logs);
        for($i=0;$i<count($logs);$i++){
            $c3++;
            if($logs[$i]["COMMENTS"])
                $str="{$logs[$i]["USERNAME"]} 审批动作：{$logs[$i]["MEMO"]}；审批意见：{$logs[$i]["COMMENTS"]}(审批时间：{$logs[$i]["ACTIONTIME"]})";
            else
                $str="{$logs[$i]["USERNAME"]} 审批动作：{$logs[$i]["MEMO"]}；审批意见：无 (审批时间：{$logs[$i]["ACTIONTIME"]})";
            $s->mergeCells('A'.$c3.":P".$c3);
            $s->setCellValue('A'.$c3,$str);
        }

        $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
        ob_end_clean();
        header("Pragma: public");
        header("Expires:0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");
        header('Content-Disposition:attachment;filename=完整版.xls');
        header("Content-type:text/html;charset=utf-8");
        header("Content-Transfer-Encoding:binary");
        $objWriter->save('php://output');

    }

    //返回二维数组中的某二维key的合计
    public function array_sum($arr,$key){
        $sum = 0;
        foreach($arr as $k=>$v){
            $sum += $v[$key];
        }
        return $sum;
    }
}
